home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / graphics / 3dvect30.arj / TGA2ICON.ARJ / TGA2ICON.ASM < prev    next >
Assembly Source File  |  1993-10-13  |  14KB  |  701 lines

  1.  
  2. ; Link this with FILE32, and ARGC32, and enable OPENFILE, READFILE,
  3. ; FILESIZE, CCHEKSTR, and CCHEKSWITCH in their respective libs.
  4.  
  5.         .386p
  6.         jumps
  7.  
  8. code32  segment para public use32
  9.         assume cs:code32, ds:code32, ss:code32
  10.  
  11. include pmode.inc
  12. include file.inc
  13. include argc.inc
  14.  
  15. public  _main
  16.  
  17. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  18. ; DATA
  19. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  20. errmsg0         db      'SYNTAX: TGA2ICON [-a#] filename',0dh,0ah,0dh,0ah
  21.                 db      '   -a add number to all numbers in sorted TGA output',0dh,0ah
  22.  
  23. errmsg1         db      'Error opening input file!$'
  24. errmsg2         db      'Not enough extended memory to load file!!!$'
  25.  
  26. inputname       db 60 dup (?)
  27. filelen         dd ?
  28. outputname      db 60 dup (?)
  29. labellen        dd ?
  30.  
  31. outptr          dd ?
  32. addvalue        db 5 dup (0)
  33.  
  34. pallet          dd 999 dup (?)
  35. psize           dw 0
  36.  
  37. xsize           dw 0
  38. xcount          dw 0
  39. ysize           dw 0
  40.  
  41. temp1           dw 0
  42. temp2           dw 0
  43.  
  44. inverted        db 0
  45.  
  46. chars      db "0123456789abcdef"
  47. byte2      db "0",0
  48. vc         dd 0
  49. xq         dw 0
  50. wide      dw 0 ; width checker (in case line>255 characters)
  51.  
  52. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  53. ; CODE
  54. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  55.  
  56. include strltu.rt
  57. include strhtn.rt
  58.  
  59. ;-----------------------------------------------------------------------------
  60. exiterr2:
  61.         mov edx,offset errmsg2
  62.         jmp short exiterr
  63. ;-----------------------------------------------------------------------------
  64. exiterr1:
  65.         mov edx,offset errmsg1
  66.         jmp short exiterr
  67. ;-----------------------------------------------------------------------------
  68. exiterr0:
  69.         mov edx,offset errmsg0
  70. exiterr:
  71.         call _putdosmsg
  72.         jmp _exit
  73.  
  74. ;═════════════════════════════════════════════════════════════════════════════
  75.  
  76. _main:
  77.         xor al,al                       ; chek for filename on commandline
  78.         mov edx,offset inputname
  79.         call _cchekstr
  80.         jc exiterr0
  81.  
  82.         call _openfile                  ; attempt to open file
  83.         jc exiterr1
  84.  
  85.         mov eax,4000h                   ; setup file buffer
  86.         call _getlomem
  87.         mov _filebufloc,eax
  88.  
  89.         call _filesize
  90.         mov filelen,eax
  91.  
  92.         call _gethimem                  ; chek for, and get enough himem
  93.         jc exiterr2
  94.         mov outptr,eax
  95.  
  96.         mov edx,outptr                  ; load file into himem
  97.         mov ecx,filelen                 ; restore actual filesize for load
  98.         call _readfile
  99.         call _closefile                 ; file is in memory! kowabunga!
  100.  
  101. ;═════════════════════════════════════════════════════════════════════════════
  102.  
  103. ; test if add switch used
  104.  
  105.         mov al,'a'                      ; chek for add switch
  106.         call _cchekswitch
  107.         jc short mf0
  108.  
  109.         mov edx,offset addvalue         ; misc data ptr
  110.         call _ccheksstr
  111.         call _strltu                    ; convert string to number
  112.         call _strhtn
  113.         call vct
  114.         mov addvalue,al
  115.         jmp mf1
  116. mf0:
  117.         mov addvalue,1
  118. mf1:
  119.  
  120. ; set up output file name
  121.  
  122.         mov ecx,8
  123.         mov edi,0
  124.         mov edx,0
  125. mn:
  126.         mov al,inputname[di]
  127.         cmp al,"."
  128.         je nt
  129.         inc edx
  130.         mov outputname[di],al
  131.         inc di
  132.         loop mn
  133. nt:
  134.         mov outputname[di+0],"."
  135.         mov outputname[di+1],"i"
  136.         mov outputname[di+2],"n"
  137.         mov outputname[di+3],"c"
  138.         mov outputname[di+4],0
  139.  
  140.         mov labellen,edx
  141.  
  142.         mov edx,offset outputname
  143.         call _createfile
  144.  
  145. ; find info on this tga file
  146.  
  147.         mov esi,outptr
  148.         add esi,12
  149.         lodsw
  150.         mov xsize,ax                      ; x size
  151.         lodsw
  152.         mov ysize,ax                      ; y size
  153.         lodsb
  154.         lodsb
  155.         mov inverted,al                   ; is tga inverted, 20h if so
  156.         mov outptr,esi                    ; esi = begining of file
  157.  
  158.         movzx eax,xsize
  159.         movzx ecx,ysize
  160.         mul ecx
  161.         mov ebx,3             ; 3 bytes per pel
  162.         mul ebx
  163.         mov filelen,eax
  164.  
  165. ; find number of colours in tga file
  166.  
  167.         mov ecx,filelen
  168.         mov esi,outptr
  169.         mov psize,0
  170.  
  171.         cld
  172.  
  173. n_loop:
  174.         mov eax,0            ; output matrix
  175.         lodsb
  176.         mov dl,al
  177.         lodsb
  178.         mov bl,al
  179.         lodsb
  180.         shl eax,8
  181.         mov al,bl
  182.         shl eax,8
  183.         mov al,dl
  184.         shl eax,8
  185.  
  186.         cmp eax,0
  187.         je old1
  188.  
  189.         mov di,0
  190. k_loop:
  191.         mov bp, di
  192.         shr bp, 2
  193.         cmp bp, psize
  194.         jge foundnew
  195.         cmp eax,pallet[di]
  196.         je old1
  197.         add di,4
  198.         jmp k_loop
  199.  
  200. foundnew:
  201.         mov pallet[edi],eax
  202.         inc psize
  203.         cmp psize,999
  204.         je outfast
  205. old1:
  206.         sub ecx,3
  207.         cmp ecx,0
  208.         ja n_loop
  209. outfast:
  210.  
  211. ; write number of colours detected:x
  212. ; then write pallet lable
  213.  
  214.         call write_number
  215.  
  216.         mov ax,psize
  217.         call write_ax_asdec
  218.         call write_ret
  219.         call write_ret
  220.  
  221.         mov ecx,labellen                  ; write output name as label
  222.         mov edx,offset outputname
  223.         call _writefile
  224.         call writepallet
  225.  
  226.         cmp psize,255
  227.         jne notfull_size
  228.         call write_db
  229.  
  230.           mov al,"0"  ; 256 colours in file?, full size, set 0 colour
  231.           call writeit
  232.           call write_comma
  233.           mov al,"0"
  234.           call writeit
  235.           call write_comma
  236.           mov al,"0"
  237.           call writeit
  238.           call write_ret
  239.  
  240. notfull_size:
  241.  
  242. ; sort colours in pallet
  243.  
  244.            movzx ecx,psize
  245.  
  246.            cmp ecx,2
  247.            jle quickex
  248.  
  249. nextccx:
  250.            mov bx,0               ; sort flag
  251.            dec cx
  252.            jcxz quickex
  253.            mov si,psize
  254.            dec si
  255.            shl si,2
  256. nextddx:
  257.            sub si,4
  258.  
  259.            mov eax,pallet[si+4]
  260.            cmp eax,pallet[si]
  261.            jae donotng
  262.            xchg eax,pallet[si]
  263.            xchg eax,pallet[si+4]
  264.            inc bx                 ; flag that one sorted
  265. donotng:
  266.            cmp si,0
  267.            jne nextddx
  268.  
  269.            cmp bx,0               ; re-sort until no more sorts
  270.            jne nextccx
  271. quickex:
  272.  
  273. ; write pallet to file
  274.  
  275.           mov temp1,0
  276.           mov al,addvalue
  277.           mov ah,0
  278.           mov temp2,ax
  279.  
  280. wri_loop:
  281.           call write_db
  282.  
  283.           mov al,"0"
  284.           call writeit
  285.  
  286.           mov di,temp1
  287.           mov al,byte ptr pallet[di+3]
  288.           shr al,2
  289.           call write_al_ashex
  290.           mov al,"h"
  291.           call writeit
  292.           call write_comma
  293.           mov al,"0"
  294.           call writeit
  295.  
  296.           mov di,temp1
  297.           mov al,byte ptr pallet[di+2]
  298.           shr al,2
  299.           call write_al_ashex
  300.           mov al,"h"
  301.           call writeit
  302.           call write_comma
  303.           mov al,"0"
  304.           call writeit
  305.  
  306.           mov di,temp1
  307.           mov al,byte ptr pallet[di+1]
  308.           shr al,2
  309.           call write_al_ashex
  310.           mov al,"h"
  311.           call writeit
  312.  
  313.           add temp1,4
  314.  
  315.           mov al," "
  316.           call writeit
  317.           mov al,";"
  318.           call writeit
  319.           mov al," "
  320.           call writeit
  321.           mov ax,temp2
  322.           inc temp2
  323.           call write_ax_asdec
  324.           call write_ret
  325.  
  326.           mov ax,psize
  327.           mov bx,4
  328.           mul bx
  329.           cmp temp1,ax
  330.           jl wri_loop
  331.  
  332.         call write_ret
  333.  
  334.         mov ecx,labellen                 ; write output name as label
  335.         mov edx,offset outputname
  336.         call _writefile
  337.  
  338.         call write_dw
  339.  
  340.         mov ax,xsize
  341.         call write_ax_asdec
  342.         call write_comma
  343.         mov ax,ysize
  344.         call write_ax_asdec
  345.         call write_comment
  346.  
  347.         call write_db
  348.  
  349.         mov esi,outptr
  350.  
  351.         mov ax,xsize
  352.         mov xcount,ax
  353.         mov wide,0         ; line width counter
  354.  
  355.         cmp inverted,0
  356.         je  doup
  357.  
  358.  
  359. na_loop2:
  360.         mov eax,0            ; output matrix
  361.         lodsb
  362.         mov dl,al
  363.         lodsb
  364.         mov bl,al
  365.         lodsb
  366.         shl eax,8
  367.         mov al,bl
  368.         shl eax,8
  369.         mov al,dl
  370.         shl eax,8
  371.  
  372.         cmp eax,0
  373.         je old3
  374.  
  375.         mov di,0
  376.  
  377. k_loop2:
  378.         mov bp, di
  379.         shr bp, 2
  380.         cmp bp, psize
  381.         jge foundnew2
  382.         cmp eax,pallet[di]
  383.         je old2
  384.         add di,4
  385.         jmp k_loop2
  386.  
  387. old2:
  388.         mov ax,di
  389.         shr ax,2
  390.         add al,addvalue
  391. old3:
  392.         push esi
  393.         call write_ax_asdec
  394.         cmp wide,240
  395.         jb notaaq
  396.         call write_ret   ; print new line if line gets too long
  397.         call write_db
  398.         jmp skc
  399. notaaq:
  400.         cmp xcount,1
  401.         je skc
  402.         call write_comma
  403. skc:
  404.         pop esi
  405.  
  406. foundnew2:
  407.         dec xcount
  408.         jne na_loop2
  409.  
  410.         push esi
  411.         call write_ret
  412.         cmp ysize,1
  413.         je  ndb
  414.         call write_db
  415. ndb:
  416.         pop esi
  417.         mov ax,xsize
  418.         mov xcount,ax
  419.  
  420.         dec ysize
  421.         jne na_loop2
  422.  
  423.         call _closefile
  424.  
  425.         jmp _exit
  426.  
  427. ; do upside down tga file (byte 18 <> $20)
  428.  
  429. doup:
  430.         movzx eax,xsize
  431.         movzx ebx,ysize
  432.         mov ecx,3
  433.         dec ebx
  434.         mul ebx
  435.         mul ecx
  436.         mov esi,eax
  437.         add esi,outptr
  438.  
  439. na_loop3:
  440.         mov eax,0            ; output matrix
  441.         lodsb
  442.         mov dl,al
  443.         lodsb
  444.         mov bl,al
  445.         lodsb
  446.         shl eax,8
  447.         mov al,bl
  448.         shl eax,8
  449.         mov al,dl
  450.         shl eax,8
  451.  
  452.         cmp eax,0
  453.         je old5
  454.  
  455.         mov di,0
  456.  
  457. k_loop3:
  458.         mov bp, di
  459.         shr bp, 2
  460.         cmp bp, psize
  461.         jge foundnew3
  462.         cmp eax,pallet[di]
  463.         je old4
  464.         add di,4
  465.         jmp k_loop3
  466.  
  467. old4:
  468.         mov ax,di
  469.         shr ax,2
  470.         add al,addvalue
  471. old5:
  472.         push esi
  473.         call write_ax_asdec
  474.         cmp wide,240
  475.         jb notaaq2
  476.         call write_ret   ; print new line if line gets too long
  477.         call write_db
  478.         jmp sk1
  479. notaaq2:
  480.         cmp xcount,1
  481.         je sk1
  482.         call write_comma
  483. sk1:
  484.         pop esi
  485.  
  486. foundnew3:
  487.         dec xcount
  488.         jne na_loop3
  489.  
  490.         push esi
  491.         call write_ret
  492.         cmp ysize,1
  493.         je  nd1
  494.         call write_db
  495. nd1:
  496.         pop esi
  497.         mov ax,xsize
  498.         mov xcount,ax
  499.  
  500.         dec ysize
  501.         jne doup
  502.  
  503.         call _closefile
  504.  
  505.         jmp _exit
  506.  
  507. write_dw:
  508.            mov ecx,4
  509.            mov edx,offset textdw
  510.            call _writefile
  511.            ret
  512.  
  513. textdw     db " dw "
  514.  
  515. write_number:
  516.            mov ecx,textgf-textde
  517.            mov edx,offset textde
  518.            call _writefile
  519.            ret
  520.  
  521. textde     db "; number of colours detected: "
  522. textgf:
  523.  
  524. write_comment:
  525.            mov ecx,textgo-textdi
  526.            mov edx,offset textdi
  527.            call _writefile
  528.            ret
  529.  
  530. textdi     db " ; x,y size of icon",13,10
  531. textgo:
  532.  
  533. write_db:
  534.            mov ecx,4
  535.            mov edx,offset textdb
  536.            call _writefile
  537.            add wide,4
  538.            ret
  539.  
  540. textdb     db " db "
  541.  
  542. writepallet:
  543.            mov ecx,7
  544.            mov edx,offset textdp
  545.            call _writefile
  546.            ret
  547.  
  548. textdp     db "palette"
  549.  
  550. write_ret:
  551.            mov ecx,2
  552.            mov edx,offset textret
  553.            call _writefile
  554.            mov wide,0
  555.            ret
  556.  
  557. textret    db 13,10
  558.  
  559. write_al_ashex:
  560.            push ax
  561.            shr al,4
  562.            call dropchar
  563.            pop ax
  564.            call dropchar
  565.            ret
  566. dropchar:
  567.            and ax,0fh
  568.            mov si,ax
  569.            mov bl,chars[si]
  570.            mov byte2,bl
  571.            mov edx, offset byte2
  572.            mov ecx,1
  573.            call _writefile
  574.            add wide,1
  575.            ret
  576.  
  577. write_ax_asdec:
  578.            call _cv
  579.  
  580.            mov xq,0
  581.            mov vc,eax
  582.  
  583.            shr eax,16
  584.            call dropchar2
  585.            shr eax,12
  586.            call dropchar2
  587.            shr eax,8
  588.            call dropchar2
  589.            shr eax,4
  590.            call dropchar2
  591.            mov xq,1
  592.            call dropchar2
  593.            ret
  594.  
  595. dropchar2:
  596.            and ax,0fh
  597.            cmp ax,0
  598.            jne jo0
  599.            cmp xq,0
  600.            je nxq
  601. jo0:
  602.            mov xq,1
  603.            and ax,0fh
  604.            mov si,ax
  605.            mov bl,chars[si]
  606.            mov byte2,bl
  607.            mov edx, offset byte2
  608.            mov ecx,1
  609.            call _writefile
  610.            add wide,1
  611. nxq:
  612.            mov eax,vc
  613.            ret
  614.  
  615. _cv:
  616.         xor ecx,ecx
  617.         xor dx,dx
  618.         mov bx,10000
  619.         div bx
  620.         or cl,al
  621.         shl ecx,4
  622.         mov ax,dx
  623.         xor dx,dx
  624.         mov bx,1000
  625.         div bx
  626.         or cl,al
  627.         shl ecx,4
  628.         mov ax,dx
  629.         xor dx,dx
  630.         mov bx,100
  631.         div bx
  632.         or cl,al
  633.         shl ecx,4
  634.         mov ax,dx
  635.         xor dx,dx
  636.         mov bx,10
  637.         div bx
  638.         or cl,al
  639.         shl ecx,4
  640.         or cl,dl
  641.         mov eax,ecx
  642.         ret
  643.  
  644. vct:
  645.         push ax
  646.         shr ax,8
  647.         mov bx,100
  648.         and ax,0fh
  649.         mul bx
  650.         mov cx,ax
  651.         pop ax
  652.  
  653.         push ax
  654.         shr ax,4
  655.         mov bx,10
  656.         and ax,0fh
  657.         mul bx
  658.         add cx,ax
  659.         pop ax
  660.  
  661.         and ax,0fh
  662.         add ax,cx
  663.  
  664.         ret
  665.  
  666. write_comma:
  667.            mov al,","
  668.            call writeit
  669.            add wide,1
  670.            ret
  671. write_0:
  672.            mov al,"0"
  673.            call writeit
  674.            add wide,1
  675.            ret
  676. writeit:
  677.            mov byte2,al
  678.            mov edx,offset byte2
  679.            mov ecx,1
  680.            call _writefile
  681.            ret
  682.  
  683. _putdosmsg:
  684.         push ax
  685.         push edx
  686.         add edx,_code32a
  687.         mov al,dl
  688.         and ax,0fh
  689.         shr edx,4
  690.         mov v86r_ds,dx
  691.         mov v86r_dx,ax
  692.         mov v86r_ah,9
  693.         mov al,21h
  694.         int 33h
  695.         pop edx
  696.         pop ax
  697.         ret
  698.  
  699. code32  ends
  700.         end
  701.